/*
 * PowerDownSave.c实现参数掉电保存功能
 */
#include "head\PowerDownSave.h"

void PDS_InitStrPDS()
{
    strPowDownSave *pstrPDS_Init;
    pstrPDS = (strPowDownSave *)MSP430_ADDRESS_POW_DOWN_SAVE;
    pstrPDS_Init = (strPowDownSave *)MSP430_ADDRESS_POW_DOWN_SAVE;
    if(pstrPDS_Init->firmWareVersion != FIRM_WARE_VERSION)
    {
        bool status;
        uint8_t uData8;
        uint16_t uData16;
        int16_t data16;
        uint32_t uData32;

        do {
            FlashCtl_eraseSegment((uint8_t *)pstrPDS_Init);
            status = FlashCtl_performEraseCheck((uint8_t *)pstrPDS_Init,128);
        } while (status == STATUS_FAIL);

        //----------------------型号----------------------
        uData16 = HARD_WARE_VERSION;
        FlashCtl_write16(&uData16,(uint16_t *)&pstrPDS_Init->hardWareVersion,1);

        //----------------------序列号----------------------
        FlashCtl_write8((uint8_t *)MSP430_ADDRESS_Die_X_position,(uint8_t *)&pstrPDS_Init->msp430ID,4);

        //----------------------固件版本----------------------
        uData16 = FIRM_WARE_VERSION;
        FlashCtl_write16(&uData16,(uint16_t *)&pstrPDS_Init->firmWareVersion,1);

        //-----------------增益，实际值=参数/1000----------------
        uData16 = 1000;
        FlashCtl_write16(&uData16,(uint16_t *)&pstrPDS_Init->gain,1);

        //-----------------偏压   ，实际值=参数/100----------------
        data16 = 0;
        FlashCtl_write16((uint16_t *)&data16,(uint16_t *)&pstrPDS_Init->bias,1);

        //----------------------可见光积分---------------------
        uData32 = 20;
        FlashCtl_write32(&uData32,(uint32_t *)&pstrPDS_Init->visibleIntegral,1);

        //-----------------------短波积分----------------------
        uData32 = 100;
        FlashCtl_write32(&uData32,(uint32_t *)&pstrPDS_Init->shortwaveIntegral,1);

        //-----------------补光灯电压,实际值=参数/10----------------
        uData16 = 50;
        FlashCtl_write16(&uData16,(uint16_t *)&pstrPDS_Init->lightVoltage,1);

        //---------------定时发送信息的发送时间，单位为分钟--------------
        uData8 = 1;
        FlashCtl_write8(&uData8,(uint8_t *)&pstrPDS_Init->infoSendTime,1);

        //----------定时发送的信息所包含的详细内容条目,0xff为全部发送--------
        uData8 = 0xff;
        FlashCtl_write8(&uData8,(uint8_t *)&pstrPDS_Init->infoSendDetail,1);


        //----------低功耗使能标志，当电压检测低于阈值时，若该标志为高，则关断4G。默认为0，不关断4G--------
        uData8 = 0;
        FlashCtl_write8(&uData8,(uint8_t *)&pstrPDS_Init->lowPowerEnable,1);

        //------------------------低功耗阈值，实际值=参数/10----------------------
        uData16 = 50;
        FlashCtl_write16(&uData16,(uint16_t *)&pstrPDS_Init->lowPowerThreshold,1);

        //--------------------------电源电压检查间隔，单位秒--------------------------
        uData16 = 300;
        FlashCtl_write16(&uData16,(uint16_t *)&pstrPDS_Init->lowPoewrCheackInterval,1);

        //-------------------------低功耗模式下休眠时间，单位分钟-------------------------
        uData8 = 2;
        FlashCtl_write8(&uData8,(uint8_t *)&pstrPDS_Init->lowPowerSleep,1);

        //-------------------------低功耗模式下休眠时间，单位分钟-------------------------
        uData8 = 1;
        FlashCtl_write8(&uData8,(uint8_t *)&pstrPDS_Init->lowPowerSleepInterval,1);
    }
    strPDS = *pstrPDS;
}

void PDS_WriteStrPDS(strPowDownSave *newStrPDS)
{
    bool status;
    strPowDownSave *pstrPDS_Write;
    pstrPDS_Write = (strPowDownSave *)MSP430_ADDRESS_POW_DOWN_SAVE;

    do {
        FlashCtl_eraseSegment((uint8_t *)pstrPDS_Write);
        status = FlashCtl_performEraseCheck((uint8_t *)pstrPDS_Write,128);
    } while (status == STATUS_FAIL);

    //----------------------型号----------------------
    FlashCtl_write16((uint16_t *)&newStrPDS->hardWareVersion,
                     (uint16_t *)&pstrPDS_Write->hardWareVersion,1);

    //----------------------序列号----------------------
    FlashCtl_write8((uint8_t *)&newStrPDS->msp430ID,
                    (uint8_t *)&pstrPDS_Write->msp430ID,4);

    //----------------------固件版本----------------------
    FlashCtl_write16((uint16_t *)&newStrPDS->firmWareVersion,
                     (uint16_t *)&pstrPDS_Write->firmWareVersion,1);

    //-----------------增益，实际值=参数/1000----------------
    FlashCtl_write16((uint16_t *)&newStrPDS->gain,
                     (uint16_t *)&pstrPDS_Write->gain,1);

    //-----------------偏压   ，实际值=参数/100----------------
    FlashCtl_write16((uint16_t *)&newStrPDS->bias,
                     (uint16_t *)&pstrPDS_Write->bias,1);

    //----------------------可见光积分---------------------
    FlashCtl_write32((uint32_t *)&newStrPDS->visibleIntegral,
                     (uint32_t *)&pstrPDS_Write->visibleIntegral,1);

    //-----------------------短波积分----------------------
    FlashCtl_write32((uint32_t *)&newStrPDS->shortwaveIntegral,
                     (uint32_t *)&pstrPDS_Write->shortwaveIntegral,1);

    //-----------------补光灯电压,实际值=参数/10----------------
    FlashCtl_write16((uint16_t *)&newStrPDS->lightVoltage,
                     (uint16_t *)&pstrPDS_Write->lightVoltage,1);

    //---------------定时发送信息的发送时间，单位为分钟--------------
    FlashCtl_write8((uint8_t *)&newStrPDS->infoSendTime,
                    (uint8_t *)&pstrPDS_Write->infoSendTime,1);

    //----------定时发送的信息所包含的详细内容条目,0xff为全部发送--------
    FlashCtl_write8((uint8_t *)&newStrPDS->infoSendDetail,
                    (uint8_t *)&pstrPDS_Write->infoSendDetail,1);


    //----------低功耗使能标志，当电压检测低于阈值时，若该标志为高，则关断4G。默认为0，不关断4G--------
    FlashCtl_write8((uint8_t *)&newStrPDS->lowPowerEnable,
                    (uint8_t *)&pstrPDS_Write->lowPowerEnable,1);

    //------------------低功耗阈值，实际值=参数/10----------------
    FlashCtl_write16((uint16_t *)&newStrPDS->lowPowerThreshold,
                     (uint16_t *)&pstrPDS_Write->lowPowerThreshold,1);

    //----------------------电源电压检查间隔，单位秒----------------------
    FlashCtl_write16((uint16_t *)&newStrPDS->lowPoewrCheackInterval,
                     (uint16_t *)&pstrPDS_Write->lowPoewrCheackInterval,1);

    //---------------------低功耗模式下休眠时间，单位分钟---------------------
    FlashCtl_write8((uint8_t *)&newStrPDS->lowPowerSleep,
                    (uint8_t *)&pstrPDS_Write->lowPowerSleep,1);

    //---------------------低功耗模式下休眠时间，单位分钟---------------------
    FlashCtl_write8((uint8_t *)&newStrPDS->lowPowerSleepInterval,
                    (uint8_t *)&pstrPDS_Write->lowPowerSleepInterval,1);
}
